﻿using System.Runtime.CompilerServices;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.Validate;
namespace Shop.RemoteModel.Activity.Model
{
    public class ActivitySet
    {
        /// <summary>
        /// 活动名
        /// </summary>
        [NullValidate("shop.activity.title.null")]
        [LenValidate("shop.activity.title.len", 2, 50)]
        public string ActivityTitle { get; set; }

        /// <summary>
        /// 活动说明
        /// </summary>
        [LenValidate("shop.activity.title.len", 2, 100)]
        public string ActivityShow { get; set; }

        /// <summary>
        /// 活动开始时间
        /// </summary>
        [TimeValidate("shop.activity.begin.time.error", TimeFormat.分, 0)]
        public DateTime BeginTime { get; set; }

        /// <summary>
        /// 活动结束时间
        /// </summary>
        [TimeValidate("shop.activity.end.time.error", TimeFormat.分, 0)]
        public DateTime EndTime { get; set; }

        /// <summary>
        /// 用户范围
        /// </summary>
        [EnumValidate("shop.activity.user.range.error", typeof(ActivityUserRange))]
        public ActivityUserRange UserRange
        {
            get;
            set;
        }
        /// <summary>
        /// 用户会员等级
        /// </summary>
        [NumValidate("shop.activity.user.grade.error", 1)]
        public long? UserGrade { get; set; }

        /// <summary>
        /// 是否限制每单购买数量
        /// </summary>
        [EntrustValidate("_Check")]
        public bool IsLimitBuyNum { get; set; }

        /// <summary>
        /// 最小限制购买数量
        /// </summary>
        [NumValidate("shop.activity.min.buyNum.error", 0)]
        public short MinBuyNum { get; set; }

        /// <summary>
        /// 最大限制数量
        /// </summary>
        [NumValidate("shop.activity.max.buyNum.error", 0)]
        public short MaxBuyNum { get; set; }

        /// <summary>
        /// 优惠范围
        /// </summary>
        [EnumValidate("shop.activity.range.type.error", typeof(DiscountRangeType))]
        public DiscountRangeType Range { get; set; }

        /// <summary>
        /// 是否限购一单
        /// </summary>
        public bool IsLimitBuy { get; set; }

        [MethodImplAttribute(MethodImplOptions.NoOptimization)]
        private void _Check ()
        {
            if ( this.IsLimitBuyNum && ( this.MaxBuyNum == 0 || this.MinBuyNum == 0 || this.MaxBuyNum < this.MinBuyNum ) )
            {
                throw new ErrorException("shop.activity.buyNum.error");
            }
        }
    }
}
